package com.jy.rhin.facade.infra.controller.audit.assembler;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.serializer.SimplePropertyPreFilter;
import com.jy.rhin.domain.audit.entity.AuditInfo;
import com.jy.rhin.sdk.dto.audit.AuditInfoEventDto;
import com.jy.rhin.sdk.dto.audit.AuditInfoRespDto;
import com.jy.rhin.sdk.dto.audit.AuditParticipantDto;
import com.jy.rhin.sdk.dto.audit.AuditWrapperDto;
import org.springframework.beans.BeanUtils;

import java.util.stream.Collectors;

/**
 * @author MaYuXuan
 * @date 2022年6月21日 08:45:19
 * @Description 审计信息汇编器
 */
public class AuditInfoAssembler {

    private AuditInfoAssembler() {
    }

    public static AuditInfoRespDto toDTO(AuditInfo auditInfo) {
        AuditInfoRespDto dto = new AuditInfoRespDto();
        BeanUtils.copyProperties(auditInfo, dto);
        return dto;
    }

    public static AuditInfo toDO(AuditWrapperDto dto) {
        AuditInfo auditInfo = new AuditInfo();
        AuditInfoEventDto eventInfo = dto.getEventInfo();
        auditInfo.setFaShengSj(eventInfo.getFaQiSj());
        auditInfo.setShiJianLx(eventInfo.getShiJianLx().getCode());
        auditInfo.setShiJianJg(eventInfo.getShiJianJg());
        auditInfo.setCanYuZhe(dto.getParticipantList().stream().map(AuditParticipantDto::getUserName).collect(Collectors.joining("、")));
        auditInfo.setShenJiYuan(dto.getSourceList().stream().map(auditSourceDto -> auditSourceDto.getLeiXing().getDisplayName()).collect(Collectors.joining("、")));
        auditInfo.setXmlText(dto.getXmlText());
        SimplePropertyPreFilter filter = new SimplePropertyPreFilter();
        //xmlText字段，不需要序列化到json字符串
        filter.getExcludes().add("xmlText");
        auditInfo.setJsonText(JSON.toJSONString(dto, filter));
        if(dto.getXmlText().contains("Fault")){
            auditInfo.setShiJianJg("4");
        }
        return auditInfo;
    }

}
